package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        Long courseId = saveTeachplanDto.getCourseId();
        Long parentid = saveTeachplanDto.getParentid();
        Long id = saveTeachplanDto.getId();
        if(id==null){
            Integer maxOrder=teachplanMapper.getMaxOrder(courseId,parentid);
            if(maxOrder==null){
                maxOrder=1;
            }
            Teachplan teachplan=new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            teachplan.setOrderby(maxOrder+1);
            teachplanMapper.insert(teachplan);
        }else{
            Teachplan teachplan=teachplanMapper.selectById(saveTeachplanDto.getId());
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Override
    public void deleteTeachplan(Long teachplanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            throw new RuntimeException();
        }else if(teachplan.getParentid()!=0){
            teachplanMapper.deleteById(teachplanId);
            teachplanMediaMapper.deleteByPlanid(teachplanId);
        }else{
            List<TeachplanDto> teachplanDtos=teachplanMapper.selectTreeNodes(teachplan.getCourseId());
            for(TeachplanDto teachplanDto:teachplanDtos){
                if(Objects.equals(teachplanDto.getId(), teachplanId)){
                    if(!teachplanDto.getTeachPlanTreeNodes().isEmpty()){
                        throw new XueChengPlusException("课程计划信息还有子级信息，无法操作");
                    }else{
                        teachplanMapper.deleteById(teachplanId);
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void move(Long teachplanId,Integer flag) {
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getGrade,teachplan.getGrade());
        queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());
        List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
        Comparator<Teachplan> cmp = (s1, s2) -> s1.getOrderby() - s2.getOrderby();
        teachplans.sort(cmp);
        for(int i=0;i<teachplans.size();i++){
            if(Objects.equals(teachplans.get(i).getId(), teachplanId)){
                if (flag==0) {
                    if(i+1<teachplans.size()){
                        Integer big = teachplans.get(i + 1).getOrderby();
                        Integer small = teachplans.get(i).getOrderby();
                        Teachplan t1 = teachplanMapper.selectById(teachplans.get(i).getId());
                        Teachplan t2 = teachplanMapper.selectById(teachplans.get(i+1).getId());
                        t1.setOrderby(big);
                        t2.setOrderby(small);
                        teachplanMapper.updateById(t1);
                        teachplanMapper.updateById(t2);
                    }
                    return;
                }else{
                    if(i>0){
                        Integer big = teachplans.get(i).getOrderby();
                        Integer small = teachplans.get(i-1).getOrderby();
                        Teachplan t1 = teachplanMapper.selectById(teachplans.get(i).getId());
                        Teachplan t2 = teachplanMapper.selectById(teachplans.get(i-1).getId());
                        t1.setOrderby(small);
                        t2.setOrderby(big);
                        teachplanMapper.updateById(t1);
                        teachplanMapper.updateById(t2);
                    }
                }
            }
        }
    }

    @Transactional
    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //教学计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            XueChengPlusException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengPlusException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //课程id
        Long courseId = teachplan.getCourseId();

        //先删除原来该教学计划绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId,teachplanId));

        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }

}
